package com.tomtom.amelinium.chartservice.serializer;

import java.util.ArrayList;

import com.tomtom.amelinium.backlogservice.model.BacklogModel;
import com.tomtom.amelinium.backlogservice.model.FeatureGroup;
import com.tomtom.amelinium.chartservice.config.ChartConfig;
import com.tomtom.amelinium.chartservice.model.ChartModel;
import com.tomtom.amelinium.chartservice.model.DoubleRow;
import com.tomtom.amelinium.chartservice.model.IntRow;
import com.tomtom.amelinium.chartservice.model.IntTable;
import com.tomtom.amelinium.chartservice.model.LogRow;
import com.tomtom.amelinium.chartservice.model.LogTable;
import com.tomtom.amelinium.chartservice.model.Roadmap;
import com.tomtom.amelinium.chartservice.model.RoadmapRow;
/**
 * Transforms chart model into Google Chart Tools JSP string.
 * 
 * @author Natasza Nowak
 * 
 */
public class JSPChartSerializer {

	public String configurationSerialized = "";
	public String chartSerialized = "";
	public String roadmapSerialized = "";
	public String burnedTableSerialized = "";
	public String summarySerialized;
	public String log = "";
	
	private ArrayList<Double[]> temp = new ArrayList<Double[]>();

	/**
	 * @return serialized version of the model.
	 */
	public String serializeChartModel(ChartModel chartModel, BacklogModel backlogModel) {
		String result = "";
		serializeConfiguration(chartModel.getChartConfig());
		serializeChart(chartModel);
		serializeDateTable(chartModel.getRoadmap());
		serializeBurnedTable(backlogModel.getOverallBurnedStoryPoints());
		serializeSummary(backlogModel.getFeatureGroupsFromAllProjects());
		serializeLogTable(chartModel.getLogTable());

		return result;
	}
	
	/**
	 * @return JSP-serialized version of the chart configuration.
	 */
	public String serializeConfiguration(ChartConfig chartConfig) {
		String result = "";
		String goBack_value = ((chartConfig.isComputeIdeal_VALUE()==true) ? String.valueOf(chartConfig.getGoBack_VALUE()) : String.valueOf(chartConfig.isComputeIdeal_VALUE()));
		result += "\n";
		result += "[ 'Configuration' , ' '],";
		result += "\n[ '";
		result += chartConfig.getCurrentSprint_KEY().replace("*", "") + "' , '" + chartConfig.getCurrentSprint_VALUE();
		result += "' ],\n[ '";
		result += chartConfig.getSprintLength_KEY() + "' , '" + chartConfig.getSprintLength_VALUE() / chartConfig.getDayLength();
		result += "' ],\n[ '";
		result += chartConfig.getZeroethSprint_KEY() + "' , '" + chartConfig.getZeroethSprint_VALUE();
		result += "' ],\n[ '";
		result += chartConfig.getGoBack_KEY().replace("'", "\"") + "' , '" + goBack_value;
		result += "' ],\n[ '";
		result += chartConfig.getExtendIdeal_KEY() + "' , '" + chartConfig.isExtendIdeal_VALUE();
		result += "' ],\n[ '";
		result += chartConfig.getAddMissingToChart_KEY() + "' , '" + chartConfig.isAddMissingToChart_VALUE();
		result += "' ],\n[ '";
		result += chartConfig.getAddMissingToRoadMap_KEY() + "' , '" + chartConfig.isAddMissingToRoadMap_VALUE();
		result += "' ],\n\n";
		configurationSerialized=result;
		return result;
		
	}
	
	/**
	 * @return JSP-serialized version of the chart.
	 */
	public String serializeChart(ChartModel chartModel) {
		int tablesTotal=2;
		tablesTotal+=chartModel.getFeatureGroupsTables().size();
		
		for (IntRow row : chartModel.getBurnedPointsTable().getRows()){
			Double [] tab = new Double[tablesTotal+1];
			tab[0]=(double) row.getSprint();
			tab[1]=(double) row.getValue();
			temp.add(tab);
		}
		
		for (int i = 0; i<chartModel.getFeatureGroupsTables().size(); i++){
			IntTable table =chartModel.getFeatureGroupsTables().get(i);
			for (IntRow row : table.getRows()){
				Double [] tab = new Double[tablesTotal+1];
				tab[0]=(double) row.getSprint();
				tab[i+2]=(double) row.getValue();
				temp.add(tab);
			}
		}
		
		for (DoubleRow row : chartModel.getIdealTable().getRows()){
			Double [] tab = new Double[tablesTotal+1];
			tab[0]=(double) row.getSprint();
			tab[tablesTotal]=(double) row.getValue();
			temp.add(tab);
		}
		
		String result = "[ ' ', '";
		result+=chartModel.getBurnedPointsTable().getTitle();
		result+="', '";
		
		for (IntTable table : chartModel.getFeatureGroupsTables()){
			result+=table.getTitle();
			result+="', '";
		}
		result+=chartModel.getIdealTable().getTitle();
		result+="' ]";

		
		for (Double[] tab : temp){
			result+=",\n[ ";
			result+=tab[0];
			for (int i=1; i<tab.length; i++){
				result+=", ";
				result+=tab[i];
			}
			result+=" ]";
		}
		chartSerialized=result;
		
		return result;
	}

	public String serializeDateTable(Roadmap t) {
		String result = "";
		result += "[ 'Feature Group' , 'Sprint' , 'End of development' , 'Deployment to production' ]";
		for (RoadmapRow r : t.getRows()) {
			result += serializeDateRow(r);
		}
		result += "\n";
		roadmapSerialized = result;
		return result;
	}

	public String serializeDateRow(RoadmapRow r) {
		String result = "";
		result += ",\n[ '";
		result += r.getFeatureGroup();
		result += "' , ";
		result += r.getSprint();
		result += " , '";
		result += r.getEndOfDevelopment();
		result += "' , '";
		result += r.getDeploymentToProduction();
		result += "' ]";
		return result;
	}
	
	public String serializeBurnedTable(int burnedPoints) {
		String result = "";
		result = "[ 'Burned story points' , " + burnedPoints + " ]";
		burnedTableSerialized = result;		
		return result;
	}
	

	public String serializeSummary(ArrayList<FeatureGroup> featureGroups) {
		String result = "";
		String name = "";
		result += "[ 'Feature Group' , 'Story Points' ]";
		for (FeatureGroup fg : featureGroups) {
			name = fg.getContentLeft();
			name = name.replace("h3. ", "");
			name = name.replaceAll(" - $", "");
			name += fg.getContentRight();
			result += ",\n[ '" + name + "' , " + fg.getCummulativePoints() + " ]";
		}
		summarySerialized = result;
		return result;
	}
	
	public String serializeLogTable(LogTable logTable) {
		String result="";
		result+="[ 'Table' , 'matched?' , 'corrected?' , 'Feature Group from backlog' ]";
		for (LogRow row : logTable.getRows()){
			result += serializeLogRow(row);
		}
		log = result;
		return result;
	}

	private String serializeLogRow(LogRow r) {
		String result = "";
		result += ",\n[ '";
		result += r.getTable();
		result += "' , ";
		result += r.isMatched();
		result += " , ";
		result += r.isCorrected();
		result += " , '";
		result += r.getFgFromBacklog();
		result += "' ]";
		return result;
	}

}
